package com.wpw.dream.annotations;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class NotNullUtil {
	
	public static <T> Boolean isNotNull(T t) {
		Boolean b = true;
		Class<? extends Object> clazz = t.getClass();
		Field[] fields = t.getClass().getDeclaredFields();
		for (Field field : fields) {
			if (field.isAnnotationPresent(NotNull.class)) {
				Method method;
				try {
					method = clazz.getMethod("get" + upFirst(field.getName()), null);
					Object invoke = method.invoke(t, null);
					if (invoke == null) {
						b = false;
						break;
					}
				} catch (NoSuchMethodException | SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		return b;
	}
	
	public static String upFirst(String str) {
		return str.replace(str.substring(0, 1), str.substring(0, 1).toUpperCase());
	}
	
	public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		User user = new User();
		System.out.println(NotNullUtil.isNotNull(user));
		user.setId(1L);
		System.out.println(NotNullUtil.isNotNull(user));
	}

}
